extends Node

# 需要播放的次数
@export var numberToPlay: int = 2
# 选择随机音高

@export var min_pitch: float = .9
@export var max_pitch: float = 1.1
@export var enable_pitch_scale: bool = true
var rnd := RandomNumberGenerator.new()


func _ready() -> void:
	rnd.randomize()

func play():
	var validNodes :Array[AudioStreamPlayer] = []
	for streamPlayer:AudioStreamPlayer in get_children():
		if (!streamPlayer.playing):
			validNodes.append(streamPlayer)
	for i in numberToPlay:
		if (validNodes.size() == 0):
			break
		# 生成随机的播放顺序
		var idx := rnd.randi_range(0, validNodes.size() - 1)
		if (enable_pitch_scale):
			validNodes[idx].pitch_scale = randf_range(min_pitch, max_pitch)
		validNodes[idx].play()
		validNodes.remove_at(idx)
		
